$(function(){
	var shopId = getQueryString("shopId");
	var isEdit = shopId?true:false;
	var initUrl = "/o2oSystem/shopadmin/getshopinitinfo";//初始化店铺信息的访问地址
	var registerShopUrl = "/o2oSystem/shopadmin/registershop";//注册店铺的访问地址
	var shopInfoUrl = "/o2oSystem/shopadmin/getshopbyid?shopId="+shopId;//带sohpId查询店铺信息地址
	var editShopUrl = "/o2oSystem/shopadmin/modifyshop";//修改店铺信息的访问地址
	/**
	 * 判断是否为编辑
	 */
	if(!isEdit){
		getShopInitInfo();
	}else{
		getShopInfo(shopId);
	}
	
	/**
	 * 获取店铺初始化信息
	 * @returns
	 */
	function getShopInitInfo(){
		$.getJSON(initUrl,function(data){//ajax发送请求；请求地址，和回调函数
			if(data.success){
				var tempHtml = '';
				var tempAreaHtml = '';
				data.shopCategoryList.map(function(item,index){//遍历后台返回的店铺类别集合
					//遍历插入option的内容，填充店铺类别的信息
					tempHtml += "<option data-id='"+item.shopCategoryId+"'>"+item.shopCategoryName+"</option>";
				});
				data.areaList.map(function(item,index){//遍历后台返回的区域信息集合
					tempAreaHtml += "<option data-id='"+item.areaId+"'>"+item.areaName+"</option>";
				});
				$("#shop-category").html(tempHtml);//将拼接好的店铺选项html代码填充到页面中
				$("#area").html(tempAreaHtml);//将拼接好的区域选项html代码填充到页面中
			}
		});
	}
	/**
	 * 提交按钮单击事件
	 */
	$("#submit").click(function(){
		var shop = {};//新建一个shop对象（json格式）
		if(isEdit){
			shop.shopId = shopId;
		}
		shop.shopName = $("#shop-name").val();
		shop.shopAddr = $("#shop-addr").val();
		shop.phone = $("#shop-phone").val();
		shop.shopDesc = $("#shop-desc").val();
		shop.shopCategory = {
			shopCategoryId:$("#shop-category").find("option").not(function(){
				return !this.selected;
			}).data("id") // 获取选中的id；（双重否定==肯定）
		};
		shop.area = {
				areaId:$("#area").find("option").not(function(){
					return !this.selected;
				}).data("id")
		};
		var shopImg = $("#shop-img")[0].files[0];//获取图片的文件流
		var formData = new FormData();//使用FromData对象
		formData.append("shopStr",JSON.stringify(shop));//将json对象转换为json字符串
		formData.append("shopImg",shopImg);
		var verifyCodeActual = $("#j_kaptcha").val();//获取图片控件中输入的验证码
		if(!verifyCodeActual){
			$.toast("请输入验证码！");
			return; // 返回
		}
		formData.append("verifyCodeActual",verifyCodeActual);//将验证码添加进formData对象
		$.ajax({		//使用ajax发送数据到后台
			url: (isEdit?editShopUrl:registerShopUrl),
			type: "POST",
			data: formData,
			processData: false,
			contentType: false,
			cache: false,
			success: function(data){ //请求数据成功回调函数
				if(data.success){
					$.toast("提交成功！");
				}else{
					$.toast("提交失败");
					console.log(data.errMsg);
				}
				$("#kaptcha-img").click();//不管提交成功或失败，都更换验证码
			}
		});
	});
	/**
	 * 根据id获取店铺信息，填充
	 * @param shopId
	 * @returns
	 */
	function getShopInfo(shopId){
		$.getJSON(shopInfoUrl,function(data){
			if(data.success){
				var shop =data.shop;
				$("#shop-name").val(shop.shopName);
				$("#shop-addr").val(shop.shopAddr);
				$("#shop-phone").val(shop.phone);
				$("#shop-desc").val(shop.shopDesc);
				var shopCategory = "<option data-id='"+shop.shopCategory.shopCategoryId+"' selected>"+shop.shopCategory.shopCategoryName+"</option>";
				var tempAreaHtml = "";
				data.areaList.map(function(item,index){
					tempAreaHtml += "<option data-id='"+item.areaId+"'>"+item.areaName+"</option>";
				});
				$("#shop-category").html(shopCategory);
				$("#shop-category").attr("disabled","disabled");
				$("#area").html(tempAreaHtml);
				$("#area option[data-id='"+shop.area.areaId+"']").attr("selected","selected");
			}
		});
	}
})